package com.zxbit.flash.presenter;

import com.zxbit.flash.BuildConfig;
import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.BankCardMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.BankListResponse;
import com.zxbit.flash.response.BindedCardResponse;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.response.PersonalInfoResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class BankCardPresenter extends BasePresenter {
    private BankCardMvpView mvpView;

    public BankCardPresenter(BankCardMvpView mvpView) {
        this.mvpView = mvpView;

    }

    public void getBankInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("userUuid", UserInfo.getShowUuid());
        List<String> steps = new ArrayList<>();
        steps.add("bank_info_code");
        map.put("pageCodes", steps);
        toSubScribe(req.getBindedCardData(UserInfo.getToken(), ParamsFactory.getCommonParams(map)), new CustomConsumer<BindedCardResponse>() {
            @Override
            public void call(BindedCardResponse bindedCardResponse) {
                mvpView.onGetDataNext(bindedCardResponse);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void saveBankCard(String realName, String bankCode, String bankName, String bankCard) {
        Map<String, Object> formMap = new HashMap<>();
        formMap.put("userUuid", UserInfo.getSaveUuid());
        formMap.put("realName", realName);
        formMap.put("bankCode", bankCode);
        formMap.put("bankName", bankName);
        formMap.put("bankCard", bankCard);
        formMap.put("authPageCode", "bank_info_code");
        if (UserInfo.isUserInformationWrong())
            formMap.put("isInformationWrong", 1);
        toSubScribe(req.saveBankCardInfo(UserInfo.getToken(), ParamsFactory.getCommonParams(formMap)), new CustomConsumer<BlankResponse>() {
            @Override
            public void call(BlankResponse blankResponse) {
                mvpView.onGetDataNext(blankResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }


    public void getRealName() {
        Map<String, Object> map = new HashMap<>();
        map.put("userUuid", UserInfo.getShowUuid());
        toSubScribe(req.getPersonalInfo(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(map)), new CustomConsumer<PersonalInfoResponse>() {
            @Override
            public void call(PersonalInfoResponse personalInfoResponse) {
                mvpView.onGetDataNext(personalInfoResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }


    public void getBankList() {
        toSubScribe(req.getBankList(UserInfo.getToken(), ParamsFactory.getCommonParams(null)), new CustomConsumer<BankListResponse>() {
            @Override
            public void call(BankListResponse response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }


}
